//
//  TabBarVC.swift
//  DouYu
//
//  Created by 王鹏宇 on 2018/4/9.
//  Copyright © 2018年 王鹏宇. All rights reserved.
//

import UIKit

class TabBarVC: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        view.backgroundColor = UIColor.white
        addChildViewContrller(childVC: HomeVC(), title: "首页", normalImg: "btn_home_normal", selectedImg: "btn_home_selected")
        addChildViewContrller(childVC: LiveVC(), title: "直播", normalImg: "btn_column_normal", selectedImg: "btn_column_selected")
        addChildViewContrller(childVC: FollowVC(), title: "关注", normalImg: "btn_live_normal", selectedImg: "btn_live_selected")
        addChildViewContrller(childVC: AccountVC(), title: "我的", normalImg: "btn_user_normal", selectedImg: "btn_user_selected")
        
        self.tabBar.tintColor = UIColor.orange
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

// MARK:- 代码创建子控制器

extension TabBarVC{
    
    func addChildViewContrller(childVC : UIViewController,title : NSString, normalImg : NSString, selectedImg : NSString){
        
        self.tabBarItem.title = title as String
        childVC.title = title as String
        
        var norImg = UIImage(named: normalImg as String)
        norImg = norImg?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        var selImg = UIImage(named: selectedImg as String)
        selImg = selImg?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        
        let tabItem = UITabBarItem(title: title as String, image: norImg, selectedImage: selImg)
        
        let naVC =  BaseNavigationVC()
        naVC.tabBarItem = tabItem
        
        naVC.addChildViewController(childVC)
        
        addChildViewController(naVC)
        
    }
}

// MARK:- 加载stroyBard

extension TabBarVC {
    
    fileprivate func addChildVc(_ storyBardName : String){
        
        //通过storyboard获取控制器
        let childVC = UIStoryboard(name: storyBardName, bundle: nil).instantiateInitialViewController()!
        //将childVC作为子控制器
        addChildViewController(childVC)
    }
}

